package com.jlastu.dlxy_androidapp.presenter;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.jlastu.dlxy_androidapp.base.WeakRefHandler;
import com.jlastu.dlxy_androidapp.contract.RegisterContract;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.entity.Store;
import com.jlastu.dlxy_androidapp.entity.User;
import com.jlastu.dlxy_androidapp.view.login.RegisterActivity;

import java.util.List;
import java.util.Objects;

public class RegisterPresenter implements RegisterContract.IRegisterPresenter {
    private static final int WHAT_REGISTER_USER= 2;
    private RegisterContract.IRegisterView mRegisterView;
    private Context mContext;
    private final String TAG = "RegisterPresenter";

    public RegisterPresenter(RegisterActivity registerActivity, Context context) {
        mRegisterView = registerActivity;
        mContext = context;
    }

    @Override
    public void registerUser(User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //商品插入
                ContentValues cv = new ContentValues();
                cv.put("user_name", user.getUserName());
                cv.put("user_password", user.getUserPassword());
                Uri userUrl = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user");
                Uri insertUri = mContext.getContentResolver().insert(userUrl, cv);
                Log.d(TAG, "insertUri: "+insertUri);
                long newRowId = ContentUris.parseId(insertUri);
                Log.d(TAG, "新注册用户的id ===== " + newRowId);
                Store store = new Store();
                ContentValues scv = new ContentValues();
                scv.put("user_id",newRowId);
                scv.put("store_name",user.getUserName());

                Uri storeUrl = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/store");
                Uri sinsertUri = mContext.getContentResolver().insert(storeUrl, scv);
                long newStoreId = ContentUris.parseId(sinsertUri);
                Log.d(TAG, "sinsertUri: "+sinsertUri);
                Log.d(TAG, "新注册店铺的id ===== " + newStoreId);
            }
        }).start();
    }

    @Override
    public void getUser(String username) {
        final Integer[] list = {null};


        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                //此处异步进行数据查询
                //Uri uri = Uri.parse("content://com.jlastu.znb.provider/goods/1");
                Log.d(TAG, "UserName: " + username);
                Uri uri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/username/" + username);
                Cursor cursor = mContext.getContentResolver().query(
                        uri, null, null, null);
                try {
                    if (cursor != null && cursor.moveToFirst()) {
                        do {
                            User user = new User();
                            String user_name = cursor.getString(cursor.getColumnIndex("user_name"));
                            if (Objects.equals(user_name, username)){
                                list[0] = 1;
                            }

                        } while (cursor.moveToNext());
                    }else {
                    list[0] = 2;
                    }

                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
                Message message = Message.obtain();
                message.obj = list[0];
                message.what = WHAT_REGISTER_USER;
                mHandler.sendMessage(message);
            }
        }).start();
    }
    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_REGISTER_USER:
                    mRegisterView.showRegister((int) msg.obj);
                    Log.d(TAG, "handleMessage: " + msg.obj);
                    break;
            }
            return false;
        }
    });
}
